// app.js
import chinese from './utils/Chiness.js'
import english from './utils/English.js'
App({
  onLaunch: function (options) {
    const that = this;
    // 获取系统信息
    const systemInfo = wx.getSystemInfoSync();
    // 胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    // 导航栏高度 = 状态栏高度 + 44
    that.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
    that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
    that.globalData.menuTop = menuButtonInfo.top;
    that.globalData.menuHeight = menuButtonInfo.height;
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    wx.setStorageSync('LastLanuage', '中文')
    // var that = this
    // var uuidKey = wx.getStorageSync('uuidKey')
    // if (uuidKey == "" || uuidKey == undefined) {
    //   that.loginHttp()
    // }


    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })

   
  },

  loginHttp: function () {
    var that = this;
    var userInfo = '';
    var uuidKey
    var userId
    var isAutho
    wx.login({
      success: function (data) {
        console.log(data.code)
        var param = {
          code: data.code
        }
        wx.request({
          url: that.globalData.rootUrl + "weixin/weixinAutho",
          data: param,
          header: {
            'content-type': "application/json",
          },
          success: function (tokenRes) {
            if (tokenRes.data.code == "200") {
              uuidKey = tokenRes.data.uuidKey;
              userId = tokenRes.data.userId;
              isAutho = tokenRes.data.isAutho;
              that.globalData.uuidKey = uuidKey
              that.globalData.userId = userId
              wx.setStorage({
                key: "uuidKey",
                data: uuidKey
              })
              wx.setStorage({
                key: "userId",
                data: userId
              })
              wx.setStorage({
                key: "isAutho",
                data: isAutho
              })
              that.jump()
              wx.request({
                url: that.globalData.rootUrl + 'weixin/userInfo/getUserInfo',
                method: "GET",
                data: {
                  uuidKey: uuidKey,
                },
                success: function (res) {
                  if (res.data.code == '200') {
                    wx.setStorage({
                      key: "nickName",
                      data: res.data.data.userInfo.nickName
                    })
                    wx.setStorage({
                      key: "avatarUrl",
                      data: res.data.data.userInfo.image
                    })
                  } else {

                  }
                }
              })
            }
          }
        })
        wx.getUserInfo({
          success: function (res) {
            if (isAutho == "false") {
              wx.setStorage({
                key: "nickName",
                data: res.userInfo.nickName
              })
              wx.setStorage({
                key: "avatarUrl",
                data: res.userInfo.avatarUrl
              })
              wx.request({
                url: that.globalData.rootUrl + "weixin/getUserInfoFromWeiXin",
                data: {
                  nickName: res.userInfo.nickName,
                  avatarUrl: res.userInfo.avatarUrl,
                  gender: res.userInfo.gender,
                  uuidKey: uuidKey,
                },
                header: {
                  'content-type': "application/json",
                },
                success: function (tokenRes) {

                }
              })
            }
          },
          fail: function () {

          }
        })
      }
    })
  },
  globalData: {
    ajax: false,
    navBarHeight: 0, // 导航栏高度
    menuRight: 0, // 胶囊距右方间距（方保持左、右间距一致）
    menuTop: 0, // 胶囊距顶部间距
    menuHeight: 0, // 胶囊高度（自定义内容可与胶囊高度保证一致）
    userInfo: null,
    serviceUrl: "https://futurefarm.bbyiot.com/",
    monitorId: 0,
    stateType: "",
    grounid2: '',
    grounpIdSlect: '',
    slectgrounpid: '',
    deviceType: '',
    status: -1,
    status2: -1,
    groupIds: '',
    dateType: '',
    monitorGroupID: '',
    serviceUrl2: "https://countryside.bbyiot.com/",
    serviceUrl3: "https://zhny.bbyiot.com/",
    serviceUrl4: "https://app.bbyiot.com/",
    editvalue: '',
    alarmvalue: '',
    neweditid: '',

    cache: "",
    urli: "https://www.51cgbd.com/robot2/image/",
    rootUrl: 'https://www.51cgbd.com/robot2/',
    // wss: 'wss://www.100badou.com/robot/ws',
    //  urli: "https://robot.100badou.com/robot/image/",
    //rootUrl: 'https://robot.100badou.com/robot/',

    wss: 'wss://robot.100badou.com/robot/ws',
    uuidKey: wx.getStorageSync('uuidKey'),
    userId: wx.getStorageSync('userId'),
    register: wx.getStorageSync('register'),
    messagename: [],
    messagenr: [],
    messagenameb: [],
    messagenrb: [],
    breakpointName: [],
    breakpointTime: [],
    loada: "--加载更多--",
    toend: "--已经到底了--",
    uid: "uuidKey",

    //serviceUrl:"http://112.124.200.87:8096/",
  },
  show: function (title) {
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 1000
    })
  }
})